// pages/pay-suc/pay-suc.js
var util = require("../../utils/util.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsId: '',
        giveType: '',
        orderId: '',
        type: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.type) {
            this.setData({
                type: options.type
            })
        }
        if (options.giveType) {
            this.setData({
                giveType: options.giveType
            })
        }
        if (options.goodsId) {
            this.setData({
                goodsId: options.goodsId
            })
        }
        if (options.orderId) {
            this.setData({
                orderId: options.orderId
            })
        }
        this.getCradUrl();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },
    toIndex: function() {
        wx.switchTab({
            url: "/pages/index/index"
        })
    },
    getCradUrl: function() {
        const _this = this;
        const cardId = wx.getStorageSync("giveCardId");
        _this.setData({
            cardId: cardId
        })
        util.request(getApp().api.card.cardUrl, { id: cardId }).then(
            function(res) {
                if (res.code == 200) {
                    _this.setData({
                        shareUrl: res.data.cardShareUrl,
                    });
                } else {
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        icon: "none", //图标,
                        //duration: 1500, //延迟时间,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function() {
                        wx.hideToast();
                    }, 1500);
                    return
                }
            }
        );
    },
    onShareAppMessage: function(options) {
        var _this = this;
        const openid = wx.getStorageSync('openid');
        if (_this.data.type == 1 || _this.data.type == 6) {
            var shareObj = {
                title: '',
                path: "/pages/goods-detail/goods-detail?productId=" + _this.data.goodsId + '&openid=' + openid,
                imageUrl: 'http://hi.lengjishan.cn/new/resource/banner/banner2.jpg',
                success: function(res) {
                    if (res.errMsg == 'shareAppMessage:ok') {}
                },
                fail: function() {
                    if (res.errMsg == 'shareAppMessage:fail cancel') {} else if (res.errMsg == 'shareAppMessage:fail') {}
                },
                complete: function() {

                }
            };
            return shareObj;
        } else if (_this.data.type == 2) {
            var shareObj2 = {
                title: '',
                path: "/pages/receive-gift/receive-gift?openid=" + openid + '&orderId=' + _this.data.orderId + '&giveType=' + _this.data.giveType + '&cardId=' + _this.data.cardId,
                imageUrl: _this.data.shareUrl,
                success: function(res) {
                    if (res.errMsg == 'shareAppMessage:ok') {}
                },
                fail: function() {
                    if (res.errMsg == 'shareAppMessage:fail cancel') {} else if (res.errMsg == 'shareAppMessage:fail') {}
                },
                complete: function() {

                }
            };
            return shareObj2;
        }

    },


})